﻿namespace YidanSoft.Library.EditorUtility.WinControls
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Windows.Forms;
    using YidanSoft.Library.EditorUtility.General;

    [ToolboxItem(false)]
    public class TextComboBox : ComboBoxBase
    {
        public TextComboBox()
        {
            base.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.UserPaint, false);
        }

        public TextComboBox(bool toolBarUse) : base(toolBarUse)
        {
            base.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque | ControlStyles.UserPaint, false);
        }

        protected override void DrawComboBoxItem(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
        {
            base.DrawComboBoxItem(g, bounds, Index, selected, editSel);
            if (Index != -1)
            {
                SolidBrush brush = new SolidBrush(SystemColors.MenuText);
                Size size = TextUtil.GetTextSize(g, base.Items[Index].ToString(), this.Font);
                int y = bounds.Top + ((bounds.Height - size.Height) / 2);
                if (base.DataSource != null)
                {
                    this.DrawDataBoundMember(g, brush, Index, new Point(bounds.Left + 1, y));
                }
                else
                {
                    g.DrawString(base.Items[Index].ToString(), this.Font, brush, (PointF) new Point(bounds.Left + 1, y));
                }
                brush.Dispose();
            }
        }

        protected override void DrawComboBoxItemEx(Graphics g, Rectangle bounds, int Index, bool selected, bool editSel)
        {
            base.DrawComboBoxItemEx(g, bounds, Index, selected, editSel);
            if (Index != -1)
            {
                SolidBrush brush = new SolidBrush(SystemColors.MenuText);
                Size size = TextUtil.GetTextSize(g, base.Items[Index].ToString(), this.Font);
                int y = bounds.Top + ((bounds.Height - size.Height) / 2);
                Rectangle layoutRectangle = new Rectangle(bounds.Left + 4, y, (bounds.Width - 12) - 4, y + size.Height);
                g.DrawString(base.Items[Index].ToString(), this.Font, brush, layoutRectangle);
                brush.Dispose();
            }
        }

        protected void DrawDataBoundMember(Graphics g, Brush brush, int index, Point point)
        {
            string s = string.Empty;
            if (base.DataSource != null)
            {
                IList dataSource = null;
                if (base.DataSource is IList)
                {
                    dataSource = (IList) base.DataSource;
                    Debug.Assert(dataSource != null, "ComboBox is bound to unrecognized DataSource");
                    object obj2 = dataSource[index];
                    s = (string) obj2.GetType().GetProperty(base.DisplayMember).GetValue(obj2, null);
                }
                else if (base.DataSource is IListSource)
                {
                    dataSource = ((IListSource) base.DataSource).GetList();
                    Debug.Assert(dataSource != null, "ComboBox is bound to unrecognized DataSource");
                    DataRowView view = (DataRowView) dataSource[index];
                    s = view.Row[base.DisplayMember].ToString();
                }
                g.DrawString(s, this.Font, brush, (PointF) point);
            }
        }

        protected override void DrawDisableState()
        {
            base.DrawDisableState();
            int selectedIndex = this.SelectedIndex;
            if (selectedIndex != -1)
            {
                using (Graphics graphics = base.CreateGraphics())
                {
                    using (Brush brush = new SolidBrush(SystemColors.ControlDark))
                    {
                        Rectangle clientRectangle = base.ClientRectangle;
                        Size size = TextUtil.GetTextSize(graphics, base.Items[selectedIndex].ToString(), this.Font);
                        int y = clientRectangle.Top + ((clientRectangle.Height - size.Height) / 2);
                        Rectangle layoutRectangle = new Rectangle(clientRectangle.Left + 4, y, ((clientRectangle.Width - 4) - 12) - 4, y + size.Height);
                        graphics.DrawString(base.Items[selectedIndex].ToString(), this.Font, brush, layoutRectangle);
                    }
                }
            }
        }
    }
}

